package com.kgc.scd.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kgc.scd.config.UserCollectConfig;
import com.kgc.scd.constant.ShareWebConstant;
import com.kgc.scd.service.ShareWebService;
import com.kgc.scd.service.SignInServerService;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.SnowUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * Created On : 2022/11/17.
 * <p>
 * Author : yantao
 * <p>
 * Description: ShareWebServiceImpl
 */
@Slf4j
@Service
public class ShareWebServiceImpl implements ShareWebService {
    @Autowired
    private SignInServerService signInServerService;

    @Autowired
    private UserCollectConfig userCollectConfig;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SnowUtil snowUtil;
    @Override
    public String generateShareCode(String userId, String goodsId) {
        String code = snowUtil.snowId();

        JSONObject json = new JSONObject();
        json.put("userId",userId);
        json.put("goodsId",goodsId);
        json.toString();




        if (ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserGenerateSharingLinksPrefix()+":"+userId+":"+goodsId))){
            redisUtils.set(userCollectConfig.getUserGenerateSharingLinksPrefix()+":"+userId+":"+goodsId,code,200);

            redisUtils.set(userCollectConfig.getUserCodeCorrespondShare()+":"+code,json,200);
            StringBuilder uri = new StringBuilder();
            uri.append(ShareWebConstant.SHARE_LINKS_USERID_GOODSID+"?")
                    .append(ShareWebConstant.SHARE_LINKS_CODE_NAME+"="+code);
            return uri.toString();
        }

        StringBuilder uri = new StringBuilder();
        uri.append(ShareWebConstant.SHARE_LINKS_USERID_GOODSID+"?")
                .append(ShareWebConstant.SHARE_LINKS_CODE_NAME+"="+redisUtils.get(userCollectConfig.getUserGenerateSharingLinksPrefix()+":"+userId+":"+goodsId).toString());


        return uri.toString();
    }

    @Override
    public String judgeLinkageOvertime(String orUserId, String goodsId,String code,String userId) {

        if (ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserGenerateSharingLinksPrefix()+":"+orUserId+":"+goodsId))){
            return "链接无效";
        }

        if (!code.equals(redisUtils.get(userCollectConfig.getUserGenerateSharingLinksPrefix()+":"+orUserId+":"+goodsId).toString())){
            return "链接码错误";
        }

        if (!ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserBindShareCodePrefix()+":"+userId+":"+goodsId))){
            return "重复点击";
        }

        return null;
    }



    @Override
    @Async
    public void userBindCode(String userId, String goodsId,String code,String orUserId) {
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("orUserId",orUserId);
        json.toString();
        redisUtils.set(userCollectConfig.getUserBindShareCodePrefix()+":"+userId+":"+goodsId,json);
    }

    @Override
    @Async
    public void userAddIntegral(String userId, String goodsId) {


        if (ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserBindShareCodePrefix()+":"+userId+":"+goodsId))){
            return;
        }

        JSONObject json = JSONObject.parseObject(redisUtils.get(userCollectConfig.getUserBindShareCodePrefix()+":"+userId+":"+goodsId).toString());
        Map<String, Object> map = json;
        Integer a = Integer.parseInt(map.get("code").toString());
        String orUserId= map.get("orUserId").toString();

        if (a==0){
            signInServerService.addLargePointsToUsersServer(orUserId);
            json.put("code",1);
        }

        redisUtils.set(userCollectConfig.getUserBindShareCodePrefix()+":"+userId+":"+goodsId,json);

    }


}
